<?php

namespace app\index\controller\subscribemessage;

use app\common\service\DouyinMiniProgramMessageService;
use app\common\service\MiniProgramMessageService;
use app\common\service\SubscribeMessageService;
use ShidianUtil\Thinkphp\Response\ApiResponse;
use think\APP;
use think\Request as Request;

/**
 * Class Template
 * @package app\index\controller\subscribemessage
 */
class Template
{
    // 请求参数
    private Request $request;

    public function __construct(Request $request)
    {
        $this->request = $request;
    }

    /**
     * 订阅消息列表
     * @return ApiResponse
     * @url /subscribemessage/template/index
     */
    public function index(): ApiResponse
    {
        $request = $this->request;

        // 返回客户端订阅消息
        $subscribe_message_templates = [];
        $service = $this->getSubscribeMessageService($request->header('pf'));
        if ($service) {
            $subscribe_message_templates = $service->getConsultingTemplates();
        }

        return ApiResponse::success($subscribe_message_templates);
    }

    private function getSubscribeMessageService($pf): ?SubscribeMessageService
    {
        if ($pf == PT_WX) {
            return \app(MiniProgramMessageService::class);
        }

        if ($pf == PT_TT) {
            return \app(DouyinMiniProgramMessageService::class);
        }

        return null;
    }
}
